package pri.zxx.learndemo.javabase.heap;

/**
 * 堆数据结构
 */
public class HeapTest {

	private int[] array;
	private int size;

	public HeapTest(int capcity) {
		this.array = new int[capcity];
	}

	/**
	 * 建堆
	 */
	private void heapify() {
		//找到最后的非叶子节点  size/2-1
		for (int i = size / 2 - 1; i >= 0; i--) {
			down(i);
		}
	}

	/**
	 * 下潜元素
	 */
	private void down(int parent) {
		int left = parent * 2 + 1;
		int right = left + 1;
		int max = parent;
		if (left < size && array[left] > array[max]) {
			max = left;
		}
		if (right < size && array[right] > array[max]) {
			max = right;
		}
		if (max != parent) {
			swap(max, parent);
		}
	}

	private void swap(int i, int j) {
		int t = array[i];
		array[i] = array[j];
		array[j] = t;
	}

	public int poll() {
		return 0;
	}

	public int poll(int index) {
		return 0;
	}

}
